import {
    coupon
} from '../../../js/modules/market';
import {
    router
} from "../../../js/router";
import {
    confirm
} from "../../../js/newUtil";
const componentBehavior = require('../../../js/common/ComponentBehavior')
const app = getApp();
Component({
    behaviors: [componentBehavior],
    /**
     * 组件的属性列表
     */
    properties: {
        productId: {
            type: String,
            value: ''
        }
    },
    lifetimes: {
        attached() {
            this.getList()
        }
    },
    /**
     * 组件的初始数据
     */
    data: {
        contentlist: []
    },

    /**
     * 组件的方法列表
     */
    methods: {
        /**加载可领取优惠劵列表 */
        getList() {
            let that = this;
            let param = {
                ProductId: this.properties.productId,
                PageIndex: 1,
                PageSize: 2,
            };
            coupon.loadOnlineCouponsList(param, function (res) {
                console.log('子组件返回数据', res)
                if (res.count > 0) {
                    var contentlist = res.data;
                    contentlist.map((item, i) => {
                        coupon.praseCouponStatus(item);
                        item.TerminalDesc = coupon.praseTerminalName(item.Terminal);
                    });
                    that.setData({
                        couponList: contentlist
                    })


                }
            });
        },
        handleToMore() {
            this.triggerEvent('toMore')
        },
        //弹出说明
        handleShowExplain(e){
          
            let item=e.currentTarget.dataset.item
            let that=this
            coupon.getCouponInstructions({ ruleId: item.Id, isApply: item.IsApply}, function (content) {
                
                that.triggerEvent('explainData',JSON.stringify(content.describes))
               
              });
            
        },
        /**
         * 使用优惠券
         */
        onUseEvent: function (e) {
            var index = e.currentTarget.dataset.index;
            var item = this.data.contentlist[index];
            app.globalData.product_list.rule_id = item.Id;
            router.navigate({
                path: "productList"
            });
        },
        /**领取优惠劵 */
        receivedCoupon: async function (evt) {
            var couponId = evt.currentTarget.dataset.couponid;
            var fkid = evt.currentTarget.dataset.fkid;
            var that = this;
            let type = evt.currentTarget.dataset.type
            let integration = evt.currentTarget.dataset.integration
            if (type == 5) {
                // 积分兑换时，需要用户二次确认是否消耗积分兑换
                let confirmRes = await confirm({
                    content: `请确认是否需要使用${integration}积分兑换该优惠券，兑换后积分不会退还，是否兑换？`
                })
                if (!confirmRes.confirm) {
                    return
                }
            }
            coupon.onlineReceivedCoupon(couponId, fkid, function (res) {

                setTimeout(() => {
                    that.getList();
                }, 1000)

            }, type);
        },
    }
})